use crate::kvdb::traits::{DB, DBError};

use crate::kvdb::mock_db::MockDB;




pub fn open(name: &str) -> Result<Box<dyn DB>, DBError> {
    let mut db_box = Box::new(MockDB::new(name)) as Box<dyn DB>;
    db_box.open().and(Ok(db_box))
}

/* TODO:
pub fn destroy(name: &str) -> Result<bool, DBError> {
}
*/

#[cfg(test)]
mod test{
    use super::*;

    #[test] // test_syntax
    fn test_box_mut_imm(){
        let b = Box::new(10);
        // move object to immut pointer
        let const_b = b;
        assert_eq!(10, *const_b);

        //and then move object to mut pointer 
        let mut mut_b = const_b; 
        *mut_b = 20;
        assert_eq!(20, *mut_b);
    }

    #[test]
    fn test_open(){
        assert!(open("hello").map(|mut db| db.close()).is_ok());

        //DBCreator::new(Mock).open("db").close();
        //DBCreator::new(Mock).destroy("db");

    }

}
